#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
ll fastpow(ll b, ll p, ll mod) {
  ll res = 1 % mod;
  while (p) {
    if (p & 1) res = res * b % mod;
    b = b * b % mod;
    p >>= 1;
  }
  return res;
}
ll bsgs(ll a, ll n) {
  ll k = sqrt(n) + 1;
  map<ll, ll> mp;
  ll x = 1;
  rep(i, 0, k - 1) mp[x] = i, x = x * a % n;
  a = x, x = 1;
  rep(i, 1, k) {
    x = x * a % n;
    if (mp.count(x)) return i * k - mp[x];
  }
  return -1;
}
void solve() {
  cin >> n;
  n *= 9;
  if (n % 2 == 0) n /= 2;
  if (n % 2 == 0 || n % 5 == 0) {
    cout << -1 << endl;
    return;
  }
  cout << bsgs(10, n) << endl;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}